package com.pine.dbant.plugin.sqlite.function;

import com.pine.dbant.common.model.JdbcMetadataTreeView;
import com.pine.dbant.common.plugin.meta.*;
import com.pine.sunflower.core.utils.Strings;

import java.util.Arrays;
import java.util.List;

/**
 * @author xiaoyuan
 * @create 2022/12/27 13:58
 **/
public class SqliteCatalogChildrenFunction extends AbstractQueryMetadataFunction<JdbcMetadataAnalysisConfig, List> {
    public SqliteCatalogChildrenFunction(String type) {
        super(type);
    }

    @Override
    public List<MetadataNode> doQuery(JdbcMetadataAnalysisConfig metadataAnalysisConfig, QueryMetadataParam queryMetadataParam) {
        String catalog = queryMetadataParam.getConfiguration().getNecessaryValue(CATALOG);
        return Arrays.asList(new DefaultMetadataNode().setName(JdbcMetadataTreeView.TABLE_TYPE.getType())
                        .setType(JdbcMetadataTreeView.TABLE_TYPE.getType())
                        .setId(Strings.format("{}_{}_{}",metadataAnalysisConfig.getConnectionInfo().getId(),catalog,JdbcMetadataTreeView.TABLE_TYPE.getType()))
                        .addProperty(CONN_ID, metadataAnalysisConfig.getConnectionInfo().getId())
                        .addProperty(CATALOG, catalog),
                new DefaultMetadataNode().setName(JdbcMetadataTreeView.VIEW_TYPE.getType())
                        .setType(JdbcMetadataTreeView.VIEW_TYPE.getType())
                        .setId(Strings.format("{}_{}_{}",metadataAnalysisConfig.getConnectionInfo().getId(),catalog,JdbcMetadataTreeView.VIEW_TYPE.getType()))
                        .addProperty(CONN_ID, metadataAnalysisConfig.getConnectionInfo().getId())
                        .addProperty(CATALOG, catalog),
                new DefaultMetadataNode().setName(JdbcMetadataTreeView.TRIGGER_TYPE.getType())
                        .setType(JdbcMetadataTreeView.TRIGGER_TYPE.getType())
                        .setId(Strings.format("{}_{}_{}",metadataAnalysisConfig.getConnectionInfo().getId(),catalog,JdbcMetadataTreeView.TRIGGER_TYPE.getType()))
                        .addProperty(CONN_ID, metadataAnalysisConfig.getConnectionInfo().getId())
                        .addProperty(CATALOG, catalog),
                new DefaultMetadataNode().setName(JdbcMetadataTreeView.QUERY_TYPE.getType())
                        .setType(JdbcMetadataTreeView.QUERY_TYPE.getType())
                        .setId(Strings.format("{}_{}_{}",metadataAnalysisConfig.getConnectionInfo().getId(),catalog,JdbcMetadataTreeView.QUERY_TYPE.getType()))
                        .addProperty(CONN_ID, metadataAnalysisConfig.getConnectionInfo().getId())
                        .addProperty(CATALOG, catalog));
    }
}
